package com.anyou.mall.fin.service.impl;

import java.util.Date;
import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.core.domain.entity.SysUser;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.fin.domain.StoreReconciliation;
import com.anyou.mall.fin.param.CertParam;
import com.anyou.mall.fin.param.IsPassParam;
import com.anyou.mall.fin.service.IStoreReconciliationService;
import com.anyou.mall.sho.domain.CarPartsShop;
import com.anyou.mall.sho.mapper.CarPartsShopMapper;
import com.anyou.mall.sho.service.ICarPartsShopService;
import com.anyou.mall.use.domain.UserSystemNotice;
import com.anyou.mall.use.mapper.UserSystemNoticeMapper;
import com.anyou.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.fin.mapper.StoreWithdrawalAduitMapper;
import com.anyou.mall.fin.domain.StoreWithdrawalAduit;
import com.anyou.mall.fin.service.IStoreWithdrawalAduitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商家提现Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-04-19
 */
@Service
public class StoreWithdrawalAduitServiceImpl extends ServiceImpl<StoreWithdrawalAduitMapper,StoreWithdrawalAduit> implements IStoreWithdrawalAduitService
{
    @Autowired
    private StoreWithdrawalAduitMapper storeWithdrawalAduitMapper;
    @Autowired
    private CarPartsShopMapper carPartsShopMapper;
    @Autowired
    private UserSystemNoticeMapper userSystemNoticeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private IStoreWithdrawalAduitService storeWithdrawalAduitService;

    @Autowired
    private IStoreReconciliationService storeReconciliationService;


    /**
     * 查询商家提现
     *
     * @param storeWithdrawalAduitId 商家提现主键
     * @return 商家提现
     */
    @Override
    public StoreWithdrawalAduit selectStoreWithdrawalAduitByStoreWithdrawalAduitId(Long storeWithdrawalAduitId)
    {
        return storeWithdrawalAduitMapper.selectStoreWithdrawalAduitByStoreWithdrawalAduitId(storeWithdrawalAduitId);
    }

    /**
     * 查询商家提现申请列表
     *
     * @param storeWithdrawalAduit 商家提现
     * @return 商家提现申请
     */
    @Override
    public List<StoreWithdrawalAduit> selectStoreWithdrawalAduitList(StoreWithdrawalAduit storeWithdrawalAduit)
    {
        return storeWithdrawalAduitMapper.selectStoreWithdrawalAduitList(storeWithdrawalAduit);
    }

    /**
     * 新增商家提现
     *
     * @param storeWithdrawalAduit 商家提现
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertStoreWithdrawalAduit(StoreWithdrawalAduit storeWithdrawalAduit)
    {
        storeWithdrawalAduit.setCreateTime(DateUtils.getNowDate());
        storeWithdrawalAduitService.save(storeWithdrawalAduit);
        // 将提现信息插入到fin_store_reconciliation
        StoreReconciliation storeReconciliation = new StoreReconciliation();
        storeReconciliation.setStoreWithdrawalAduitId(storeWithdrawalAduit.getStoreWithdrawalAduitId());
        storeReconciliation.setCarPartsShopId(storeWithdrawalAduit.getCarPartsShopId());
        boolean save = storeReconciliationService.save(storeReconciliation);

        return true;
    }

    /**
     * 修改商家提现
     *
     * @param storeWithdrawalAduit 商家提现
     * @return 结果
     */
    @Override
    public int updateStoreWithdrawalAduit(StoreWithdrawalAduit storeWithdrawalAduit)
    {
        storeWithdrawalAduit.setUpdateTime(DateUtils.getNowDate());
        return storeWithdrawalAduitMapper.updateStoreWithdrawalAduit(storeWithdrawalAduit);
    }

    /**
     * 批量删除商家提现
     *
     * @param storeWithdrawalAduitIds 需要删除的商家提现主键
     * @return 结果
     */
    @Override
    public int deleteStoreWithdrawalAduitByStoreWithdrawalAduitIds(Long[] storeWithdrawalAduitIds)
    {
        return storeWithdrawalAduitMapper.deleteStoreWithdrawalAduitByStoreWithdrawalAduitIds(storeWithdrawalAduitIds);
    }

    /**
     * 删除商家提现信息
     *
     * @param storeWithdrawalAduitId 商家提现主键
     * @return 结果
     */
    @Override
    public int deleteStoreWithdrawalAduitByStoreWithdrawalAduitId(Long storeWithdrawalAduitId)
    {
        return storeWithdrawalAduitMapper.deleteStoreWithdrawalAduitByStoreWithdrawalAduitId(storeWithdrawalAduitId);
    }

    /**
     * 审核
     * @param isPassParam
     * @return
     */
    @Override
    public ApiResult isPass(IsPassParam isPassParam) {
        StoreWithdrawalAduit swa = storeWithdrawalAduitMapper.selectStoreWithdrawalAduitByStoreWithdrawalAduitId(isPassParam.getStoreWithdrawalAduitId());
        if (swa != null) {
            swa.setAuditStatus(isPassParam.getAuditStatus());
            swa.setFailReason(isPassParam.getFailReason());
            if(isPassParam.getAuditStatus().equals("1")){
                swa.setArrivalStatus("0");
            }
            int i = storeWithdrawalAduitMapper.updateById(swa);
            return ApiResult.success();
        } else {
            return ApiResult.error();
        }
    }
    /**
     * 提交转账凭证
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult editCert(CertParam param) {
        StoreWithdrawalAduit swa = storeWithdrawalAduitMapper.selectById(param.getStoreWithdrawalAduitId());
        if(swa!=null){
            swa.setCertImg(param.getCertImg());
            swa.setArrivalStatus("1");
            int i = storeWithdrawalAduitMapper.updateById(swa);
            if(i>0){
                //给合伙人新增一条消息
                UserSystemNotice usn = new UserSystemNotice();
                usn.setContent("你于"+DateUtils.getStringByDateTime(swa.getCreateTime())
                        +",申请的提现金额"+swa.getWithdrawalAmount()+"元,已到账,请注意查收！");
                usn.setSourceId(swa.getStoreWithdrawalAduitId());
                usn.setSkipType("3");
                usn.setNoticeType(3);
                Long sysUserId=0L;
                SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                        .eq(SysUser::getUserType, "11")
                        .eq(SysUser::getCarPartsShopId, swa.getCarPartsShopId()));
                if(sysUser!=null){
                    sysUserId=sysUser.getUserId();
                }
                usn.setSysUserId(sysUserId);
                usn.setCreateTime(new Date());
                userSystemNoticeMapper.insert(usn);
            }
        }
        return ApiResult.success();
    }
}
